package com.study.patterns.proxy.dynamic.jdk;

import com.study.patterns.proxy.dynamic.ComputeService;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by tan on 2017/6/16.
 */
public class ServiceInvocationHandlerImpl implements InvocationHandler {
	private Object target;

	public ServiceInvocationHandlerImpl(Object target) {
		this.target = target;
	}

	@Override
	//回调方法
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println("proxy invoke");
		Object result = method.invoke(target, args);
		return result;
	}

	public Object getProxy() {
		//Proxy.getInvocationHandler();
		return Proxy.newProxyInstance(target.getClass().getClassLoader(), new Class<?>[]{ComputeService.class}, this);
	}
}
